using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using SJXmlP.XMLClasses;
using System;
using System.Collections;
using System.Runtime.CompilerServices;
using System.Threading;
using yW5Lib;

namespace yWriter6
{
	public class clsPrintProject
	{
		public delegate void PrintProgressEventHandler(string ProgressString);

		[CompilerGenerated]
		private PrintProgressEventHandler PrintProgressEvent;

		public event PrintProgressEventHandler PrintProgress
		{
			[CompilerGenerated]
			add
			{
				PrintProgressEventHandler printProgressEventHandler = PrintProgressEvent;
				PrintProgressEventHandler printProgressEventHandler2;
				do
				{
					printProgressEventHandler2 = printProgressEventHandler;
					PrintProgressEventHandler value2 = (PrintProgressEventHandler)Delegate.Combine(printProgressEventHandler2, value);
					printProgressEventHandler = Interlocked.CompareExchange(ref PrintProgressEvent, value2, printProgressEventHandler2);
				}
				while ((object)printProgressEventHandler != printProgressEventHandler2);
			}
			[CompilerGenerated]
			remove
			{
				PrintProgressEventHandler printProgressEventHandler = PrintProgressEvent;
				PrintProgressEventHandler printProgressEventHandler2;
				do
				{
					printProgressEventHandler2 = printProgressEventHandler;
					PrintProgressEventHandler value2 = (PrintProgressEventHandler)Delegate.Remove(printProgressEventHandler2, value);
					printProgressEventHandler = Interlocked.CompareExchange(ref PrintProgressEvent, value2, printProgressEventHandler2);
				}
				while ((object)printProgressEventHandler != printProgressEventHandler2);
			}
		}

		public void PrintProject(clsProject cProject)
		{
			if (cProject.ChapterCount != 0)
			{
				clsCollection<int> clsCollection = new clsCollection<int>();
				frmPickChapters frmPickChapters = new frmPickChapters(ref cProject);
				frmPickChapters.EnableTagFilter();
				frmPickChapters.EnableVPFilter();
				frmPickChapters.ShowDialog();
				if (!frmPickChapters.Cancelled)
				{
					clsCollection = frmPickChapters.PickedIDs;
					clsPrintChapter clsPrintChapter = new clsPrintChapter();
					clsPrintChapter.PrintProgress += Progress;
					PrintProgressEvent?.Invoke("Printing project ...");
					string tags = frmPickChapters.Tags;
					clsPrintChapter.ClearIDs();
					clsShared.cSubs.BusyMouse();
					bool sceneTextOnly = modPrinterSubs.AskIfSceneTextOnly();
					IEnumerator enumerator = default(IEnumerator);
					try
					{
						enumerator = clsCollection.GetEnumerator();
						while (enumerator.MoveNext())
						{
							int iD = Conversions.ToInteger(enumerator.Current);
							clsChapter clsChapter = cProject.get_ChapterByID(iD);
							if (Operators.CompareString(tags, "", TextCompare: false) == 0)
							{
								clsPrintChapter.AddID(clsChapter.ID);
							}
							else
							{
								bool flag = false;
								int sceneCount = clsChapter.SceneCount;
								for (int i = 1; i <= sceneCount; i++)
								{
									clsScene clsScene = cProject.get_SceneByID(clsChapter.get_SceneIDByIndex(i));
									if ((frmPickChapters.WithUnused | !clsScene.Unused) && ((frmPickChapters.VPID <= 0) | (frmPickChapters.VPID == clsScene.ViewPointID)) && ((Operators.CompareString(frmPickChapters.Tags.Trim(), "", TextCompare: false) == 0) | clsScene.PassesTagFilter(frmPickChapters.Tags)))
									{
										flag = true;
										break;
									}
								}
								if (flag)
								{
									clsPrintChapter.AddID(clsChapter.ID);
								}
							}
						}
					}
					finally
					{
						if (enumerator is IDisposable)
						{
							(enumerator as IDisposable).Dispose();
						}
					}
					clsPrintChapter.PrintChapters(cProject, sceneTextOnly, frmPickChapters.Tags, frmPickChapters.VPID);
					clsPrintChapter.PrintProgress -= Progress;
				}
			}
		}

		private void Progress(string ProgressString)
		{
			PrintProgressEvent?.Invoke(ProgressString);
		}
	}
}
